package com.fanqwz.hexieshe.adapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fanqwz.hexieshe.BR;
import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.databinding.CommentItemBinding;
import com.fanqwz.hexieshe.model.CommentModel;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by fanqw on 2017/7/7.
 */

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.Viewholder> {
    private List<CommentModel> comments;

    @Inject
    public CommentAdapter() {

    }

    public void setComments(List<CommentModel> comments) {
        this.comments = comments;
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        CommentItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.comment_item, parent, false);
        return new Viewholder(binding.getRoot(), binding);
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position) {
        if (comments != null) {
            holder.binding.setVariable(BR.comment, comments.get(position));
            holder.binding.executePendingBindings();
        }
    }

    @Override
    public int getItemCount() {
        return comments == null ? 0 : comments.size();
    }

    class Viewholder extends RecyclerView.ViewHolder {
        CommentItemBinding binding;

        public Viewholder(View itemView, CommentItemBinding binding) {
            super(itemView);
            this.binding = binding;
        }
    }
}
